﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;

namespace ERPForms
{
    public partial class UnitatiInfoFrm : FrmCard
    {
        private short _codUnitate = -1;
        public UnitatiInfoFrm(ModuleClass mc,short cU): base(mc)
        {
            _codUnitate = cU;
            InitializeComponent();
            workConnString = mc.workConnectionString;
            _AllowAddRow = false;
        }

        private void UnitatiInfoFrm_Load(object sender, EventArgs e)
        {
            

            AddBindingSource(unitatiBindingSource);
            this.listTableAdaptor.Add(this.unitatiTableAdapter.Adapter);

            SqlConnection con = DeschideConexiune(workConnString);
            this.banciTableAdapter.Connection = con;
            this.banciTableAdapter.Fill(this.eSRPDataSet.Banci);
            this.judeteTableAdapter.Connection = con;
            this.judeteTableAdapter.Fill(this.eSRPDataSet.Judete);
            this.unitatiTableAdapter.Connection = con;
            this.unitatiTableAdapter.FillByCodUnitate(this.eSRPDataSet.Unitati,_codUnitate);
            InchideConexiune(con);
            unitatiCumulativeBindingSource.Filter = "Cumulativa=1";

        }

        private void emailTextBox_Validating(object sender, CancelEventArgs e)
        {
            string errorMsg;
            TextBox textBox1 = sender as TextBox;
            if(!ValidEmailAddress(textBox1.Text, out errorMsg))
            {
                MessageBox.Show(errorMsg,"Eroare",MessageBoxButtons.OK,MessageBoxIcon.Stop);
                // Cancel the event and select the text to be corrected by the user.
                e.Cancel = true;
                textBox1.Select(0, textBox1.Text.Length); 
            }
        }


        public bool ValidEmailAddress(string emailAddress, out string errorMessage)
        {
            // Confirm that the e-mail address string is not empty.
            if(emailAddress.Length == 0)
            {
                errorMessage = "Adresa de mail este un camp obligatoriu. Va rugam sa-l completati cu o adresa de mail valida!";
                return false;
            }
            string expression = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
            System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(expression); 
            if(regex.IsMatch(emailAddress))
            {
                errorMessage = "";
                return true;
            } 

            errorMessage = "Adresa de mail trebuie sa aiba un format valid.\n" +
            "De exemplu: 'someone@example.com' ";
            return false;
        
        }

        private void CumulativaCB_CheckedChanged(object sender, EventArgs e)
        {
            UnitateParinteCB.Enabled = !CumulativaCB.Checked;
        }

        private void telefonReprezentantLabel_Click(object sender, EventArgs e)
        {

        }

        private void telefonReprezentantTextBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void adresaReprezentantLabel_Click(object sender, EventArgs e)
        {

        }

        private void adresaReprezentantTextBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void cNPReprezentantLabel_Click(object sender, EventArgs e)
        {

        }

        private void cNPReprezentantTextBox_TextChanged(object sender, EventArgs e)
        {

        }
            
    }
        
}
